package com.wd.gui;

import com.wd.dominio.Producto;
import com.wd.dominio.Proveedor;
import com.wd.gui.controlparticular.ControlGuiProducto;
import com.wd.gui.controlparticular.ControlGuiProveedor;
import java.util.Collection;
import java.util.Vector;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Gabylis
 */
public class VentanaConsultarPrecioProducto extends javax.swing.JFrame {

    private Vector<Producto> vecProdsAll;

    private Vector<Producto> vecProdsProve;

    private Vector<Proveedor> vecProveedores;

    private Vector<Proveedor> vecProveedoresAux;

    private ControlGuiProveedor controlProveedor = new ControlGuiProveedor();

    private ControlGuiProducto controlProducto = new ControlGuiProducto();

    private DefaultTableModel dm;

    /** Creates new form VentanaConsultarPrecioProducto */
    public VentanaConsultarPrecioProducto(Vector<Producto> result) {
        initComponents();

        Collection<Proveedor> colProve = null;
        colProve = controlProveedor.traerTodosLosProveedores();
        vecProdsAll = new Vector();
        vecProveedores = new Vector();
        vecProveedoresAux = new Vector();
        vecProdsProve = new Vector();

        for (Proveedor proveedor : colProve) {
            vecProveedores.add(proveedor);
        }


        vecProdsAll = result;

        for (Producto producto : result) {
            this.comboProducts.addItem(producto.getNombre());
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        comboProducts = new javax.swing.JComboBox();
        labelProductos = new javax.swing.JLabel();
        labelIdNomb = new javax.swing.JLabel();
        labelNombreNomb = new javax.swing.JLabel();
        labelDescripcion = new javax.swing.JLabel();
        labelId = new javax.swing.JLabel();
        labelNombre = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        campoDescripcion = new javax.swing.JTextArea();
        jPanel3 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        tablaPecioProve = new javax.swing.JTable();
        buttonCerrar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Consulta de Precios por Producto");

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Consultar Precios de Producto", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 11), new java.awt.Color(0, 102, 204))); // NOI18N

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Información Producto", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 11))); // NOI18N

        comboProducts.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                comboProductsActionPerformed(evt);
            }
        });

        labelProductos.setFont(new java.awt.Font("Tahoma", 1, 11));
        labelProductos.setText("Productos");

        labelIdNomb.setFont(new java.awt.Font("Tahoma", 1, 11));
        labelIdNomb.setText("ID: ");

        labelNombreNomb.setFont(new java.awt.Font("Tahoma", 1, 11));
        labelNombreNomb.setText("Nombre:");

        labelDescripcion.setFont(new java.awt.Font("Tahoma", 1, 11));
        labelDescripcion.setText("Descripción:");

        labelId.setFont(new java.awt.Font("Tahoma", 1, 11));
        labelId.setForeground(new java.awt.Color(0, 102, 204));
        labelId.setText("ID");

        labelNombre.setFont(new java.awt.Font("Tahoma", 1, 11));
        labelNombre.setForeground(new java.awt.Color(0, 102, 204));
        labelNombre.setText("Nombre Producto");

        campoDescripcion.setColumns(20);
        campoDescripcion.setEditable(false);
        campoDescripcion.setFont(new java.awt.Font("Tahoma", 0, 13));
        campoDescripcion.setForeground(new java.awt.Color(0, 102, 204));
        campoDescripcion.setRows(5);
        jScrollPane1.setViewportView(campoDescripcion);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(20, 20, 20)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(labelProductos)
                    .addComponent(labelIdNomb)
                    .addComponent(labelNombreNomb)
                    .addComponent(labelDescripcion))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 100, Short.MAX_VALUE)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jScrollPane1)
                    .addComponent(labelNombre)
                    .addComponent(labelId)
                    .addComponent(comboProducts, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(29, 29, 29))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(38, 38, 38)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(comboProducts, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(labelProductos))
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelIdNomb)
                    .addComponent(labelId))
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelNombreNomb)
                    .addComponent(labelNombre))
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(labelDescripcion)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(19, Short.MAX_VALUE))
        );

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Informacion Precios y Proveedores", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 11))); // NOI18N

        tablaPecioProve.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        jScrollPane2.setViewportView(tablaPecioProve);

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 511, Short.MAX_VALUE)
            .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel3Layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 491, Short.MAX_VALUE)
                    .addContainerGap()))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 245, Short.MAX_VALUE)
            .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel3Layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 217, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(17, Short.MAX_VALUE)))
        );

        buttonCerrar.setFont(new java.awt.Font("Tahoma", 1, 11));
        buttonCerrar.setText("Cerrar Ventana");
        buttonCerrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonCerrarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(414, 414, 414)
                        .addComponent(buttonCerrar, javax.swing.GroupLayout.PREFERRED_SIZE, 168, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(23, 23, 23)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 18, Short.MAX_VALUE)
                .addComponent(buttonCerrar)
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(22, 22, 22)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void comboProductsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_comboProductsActionPerformed
        int select = -1;
        int codigoProd = -1;
        String nombre = "";
        String descripcion = "";

        select = this.comboProducts.getSelectedIndex();
        Producto prod = new Producto();
        prod = this.vecProdsAll.elementAt(select);
        codigoProd = prod.getId();
        nombre = prod.getNombre();
        descripcion = prod.getDescripcion();

        this.labelId.setText(codigoProd+"");
        this.labelNombre.setText(nombre);
        this.campoDescripcion.setText(descripcion);
        
        this.llenarTabla(codigoProd);
    }//GEN-LAST:event_comboProductsActionPerformed

    private void buttonCerrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonCerrarActionPerformed
        this.setVisible(false);
    }//GEN-LAST:event_buttonCerrarActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run(Vector<Producto> result) {
                new VentanaConsultarPrecioProducto(result).setVisible(true);
            }
            public void run() {
                throw new UnsupportedOperationException("Not supported yet.");
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton buttonCerrar;
    private javax.swing.JTextArea campoDescripcion;
    private javax.swing.JComboBox comboProducts;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JLabel labelDescripcion;
    private javax.swing.JLabel labelId;
    private javax.swing.JLabel labelIdNomb;
    private javax.swing.JLabel labelNombre;
    private javax.swing.JLabel labelNombreNomb;
    private javax.swing.JLabel labelProductos;
    private javax.swing.JTable tablaPecioProve;
    // End of variables declaration//GEN-END:variables

    public void llenarVecProdProve(int codigoProducto){
        this.vecProdsProve.removeAllElements();
        this.vecProveedoresAux.removeAllElements();
        vecProdsProve = this.controlProducto.traerProductoConProveedores(codigoProducto);

        for (Producto producto : vecProdsProve) {
            for (Proveedor prove : vecProveedores) {
                if(producto.getEmpresaServicioRif().equals(prove.getRif())){
                    vecProveedoresAux.add(prove);
                }
            }
        }
    }

    public void llenarTabla(int codigoProducto){

        this.llenarVecProdProve(codigoProducto);
        dm = new DefaultTableModel();
        dm.addColumn("Rif Proveedor");
        dm.addColumn("Nombre Proveedor");
        dm.addColumn("Teléfono");
        dm.addColumn("Costo Producto");
        dm.addColumn("Fecha Creación");
        dm.addColumn("Fecha Modificación");

        String fechaNull ="";
        Proveedor prove = new Proveedor();
        Producto prod = new Producto();

        for (int i = 0; i < vecProdsProve.size(); i++) {
            prove = vecProveedoresAux.elementAt(i);
            prod = vecProdsProve.elementAt(i);


            Vector info = new Vector();
            info.addElement(prove.getRif());
            info.addElement(prove.getNombre());
            info.addElement(prove.getTelefono());
            info.addElement(prod.getPrecio());
            info.addElement(prod.getFecha_creacion());
            if(prod.getFecha_ultima_modificacion()==null){
                fechaNull = "  N/A  ";
                info.addElement(fechaNull);
            }else info.addElement(prod.getFecha_ultima_modificacion());
            dm.addRow(info);
        }
        this.tablaPecioProve.setModel(dm);
    }
}
